<template>
  <el-card>
    <div slot="header">
      <span>{{ model.title.value }}</span>
    </div>
    <div
      :ref="model.id.value"
      :style="{height: model.height.value}"
    ></div>
  </el-card>
</template>

<script>
import echarts from 'echarts'

export default {
  name: 'Pie',
  props: ['model'],
  data() {
    return {
      chart: '',
      option: {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center'
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '40',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: [
              { value: 1048, name: 'Search Engine' },
              { value: 735, name: 'Direct' },
              { value: 580, name: 'Email' },
              { value: 484, name: 'Union Ads' },
              { value: 300, name: 'Video Ads' }
            ]
          }
        ]
      }
    }
  },
  mounted() {
    this.handleQuery()
  },
  watch: {
    model: {
      deep: true,
      handler(val) {
        this.onResize()
      }
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
  },
  methods: {
    handleQuery() {
      const chartDom = this.$refs[this.model.id.value]
      this.chart = echarts.init(chartDom)
      this.chart.setOption(this.option)
    },
    onResize() {
      this.chart.resize()
    }
  }
}
</script>

<style scoped>
</style>
